<?php
include('class.receiver.php');

if (isset($_POST['PRSVRC'])) {

    $qr_OBJ = $_POST['RID'];

    $qr_OBJCL = $_POST['CID'];
    $CLST = $_POST['CST'];

    $ACFi1 = 0;
    if ($_POST['BnfNombre'] == '') {
        $Msg_ERROR = 'RC002';
    } else {
        $ACFi2 = req_form(unichars($_POST['BnfNombre']));
    }
    if ($_POST['BnfCedula'] == '' && isset($_POST['BnfDocType'])) {
        $Msg_ERROR = 'RC004';
    } elseif (!isset($_POST['BnfDocType'])) {
        $ACFi4 = 0;
        $_POST['BnfDocType'] = 0;
    } else {
        $ACFi4 = req_form($_POST['BnfCedula']);
    }

    $ACFi5 = req_form($_POST['BnfDireccion']);
    $ACFi6 = req_form($_POST['BnfBarrio']);
    if (!isset($_POST['BnfCiudad'])) {
        $Msg_ERROR = 'RC007';
    } else {
        $ACFi7 = $_POST['BnfCiudad'];
    }

    if (!isset($_POST['BnfProvincia'])) {
        $ACFi8 = 'Null';
    } else {
        $ACFi8 = $_POST['BnfProvincia'];
    }

    $ACFi9 = $_POST['BnfPais'];
    $ACFi10 = req_form($_POST['BnfTelefono']);
    $ACFi11 = req_form($_POST['BnfTelefono2']);
    $ACFi17 = $sysTime;
    $ACFi19 = $_SESSION[$SESSID];
    $ACFi20 = $_POST['BnfDocType'];
    $ACFi21 = req_form($_POST['BnfNotes']);

    if (!isset($Msg_ERROR)) {
        include($GLOBALS['BIB'] . 'Pr_db/pr_historics.mex');
        if ($qr_OBJ == 0) {

            $PRRC = $GLOBALS['MYT']->db_query("INSERT
			INTO Beneficiarios
			(BnfNumExt,
				BnfNombre,
				BnfCedula,
				BnfDireccion,
				BnfBarrio,
				BnfCiudad,
				BnfProvincia,
				BnfPais,
				BnfTelefono,
				BnfTelefono2,
				BnfFechaAlta,
				BnfUsuario,
				BnfDocType,
				BnfNotes)
			VALUES
			({$ACFi1},'{$ACFi2}','{$ACFi4}','{$ACFi5}','{$ACFi6}',{$ACFi7},
			{$ACFi8},{$ACFi9},'{$ACFi10}','{$ACFi11}','{$ACFi17}',{$ACFi19},
			{$ACFi20},'{$ACFi21}')");



            //================= Associa Bnf a Cli =====================

            $SLRC = $GLOBALS['MYT']->db_array("SELECT

			BnfNumero FROM Beneficiarios WHERE BnfUsuario = {$_SESSION[$SESSID]} AND BnfFechaAlta = '{$sysTime}'
			ORDER BY BnfNumero DESC
			LIMIT 0,1");

            $qr_OBJRC = $SLRC[1]['BnfNumero'];

            $PRRCCL = $GLOBALS['MYT']->db_query("INSERT
			INTO unionclientesbeneficiarios
			(uniCliente, uniBeneficiario)
			VALUES
			({$qr_OBJCL},{$qr_OBJRC})");
            system_hs($ACFi17, 'NEW_RECEIVER', $qr_OBJRC);
        } elseif ($qr_OBJ > 0) {

            hs_receiver($qr_OBJ, $sysTime);
            $PRRC = $GLOBALS['MYT']->db_query("UPDATE
			Beneficiarios
			SET
				BnfNumExt = {$ACFi1},
				BnfNombre = '{$ACFi2}',
				BnfCedula = '{$ACFi4}',
				BnfDireccion = '{$ACFi5}',
				BnfBarrio = '{$ACFi6}',
				BnfCiudad = {$ACFi7},
				BnfProvincia = {$ACFi8},
				BnfPais = {$ACFi9},
				BnfTelefono = '{$ACFi10}',
				BnfTelefono2 = '{$ACFi11}',
				BnfFechaAlta = '{$ACFi17}',
				BnfUsuario = {$ACFi19},
				BnfDocType = {$ACFi20}

			WHERE BnfNumero = {$qr_OBJ}");
        }
        echo '<script charset="utf-8" type="text/javascript">replaceUrl(\'ClientProp\',\'Modules/CLDJ49SJ/receiver.mex?CID=' . $qr_OBJCL . '&CST=' . $CLST . '\');</script>';
    } else {
        echo '<script charset="utf-8" type="text/javascript">
            document.getElementById(\'RCMSG\').innerHTML=\'<div class="Error">'.$error_msg[$Msg_ERROR].'</div>\';</script>';
    }
}

